#include "rotaryEncoder.h"
#include "define.h"
#include <stdio.h>
#include "systick.h"

static volatile int pluseValue[pulse_Max] = {0,0};

#define info(format,...)

static GPIO_TypeDef *PULSE_INT_PORT[pulse_Max]={PULSEA_INT_PORT,PULSEB_INT_PORT};
const  u32  PULSE_INT_PIN[pulse_Max]={PULSEA_INT_PIN,PULSEB_INT_PIN};
const  u32  PULSE_INT_MODE[pulse_Max] ={PULSEA_INT_MODE, PULSEB_INT_MODE};

static GPIO_TypeDef *PULSE_DIR_PORT[pulse_Max]={PULSEA_DIR_PORT,PULSEB_DIR_PORT};
const  u32  PULSE_DIR_PIN[pulse_Max]={PULSEA_DIR_PIN,PULSEB_DIR_PIN};



const  u32  PULSE_EXTI_IRQ[pulse_Max]={PULSEA_EXTI_IRQ,PULSEB_EXTI_IRQ};

static bool rise[pulse_Max] = {FALSE, FALSE};
static bool readPulse[pulse_Max] = {FALSE, FALSE};
extern BYTE ProductType;


static u8 READ_PLUSE_INT(u8 Pluse)
{
	return HAL_GPIO_ReadPin(PULSE_INT_PORT[Pluse], PULSE_INT_PIN[Pluse]);
}

static u8 READ_PLUSE_DIR(u8 Pluse)
{
	return HAL_GPIO_ReadPin(PULSE_DIR_PORT[Pluse], PULSE_DIR_PIN[Pluse]);
}

int getRotaryEncoderDelay(void)
{
	return 0;
}
static void readPulseData(u8 Pluse)
{
	
	int i,nn;
	char KeyOldInt=0,KeyNewInt,KeyOldDir = 0,KeyNewDir;
	char KeyCount = 0;
	

	#if 1
	char temp_int, temp_dir;
	{
		//for(i = 0; i < 500; i++) 
		for(i = 0; i < 80; i++) 
		{			
			for(nn=0; nn<100; nn++);	
			KeyNewInt = READ_PLUSE_INT(Pluse);
			KeyNewDir = READ_PLUSE_DIR(Pluse);
			if((KeyNewInt == KeyOldInt)&&(KeyNewDir == KeyOldDir))
			{
				//if(++KeyCount > 490)
				if(++KeyCount > 70)
				{
					break;
				}
			}
			else 
			{
				KeyOldInt = KeyNewInt;
				KeyOldDir = KeyNewDir;
				KeyCount = 0;
			}
		}
		
		temp_int = READ_PLUSE_INT(Pluse);
		#if 0
		//printf("1.temp_value = %d,rise[Pluse] = %d\r\n",temp_value,rise[Pluse]);
		if (temp_value && !rise[Pluse]) 
		{
			readPulse[Pluse] = FALSE;
			return;
		}
		#endif
		
		temp_dir = READ_PLUSE_DIR(Pluse);
		//printf("2.INT = %d, DIR= %d, rise[Pluse] = %d\r\n",temp_int,temp_dir, rise[Pluse]);
		#if 1
		if(temp_int == temp_dir) 
		{
			if(pluseValue[Pluse]>=0)
			{
				pluseValue[Pluse]++;
			}
			else
			{
				if(getRotaryEncoderDelay() == 0)
				{
					pluseValue[Pluse]=1;
				}
			}
			
		} 
		else 
		{
			if(pluseValue[Pluse]<=0)
			{
				pluseValue[Pluse]--;
			}
			else
			{
				if(getRotaryEncoderDelay() == 0)
				{
					pluseValue[Pluse]=-1;
				}
			}
		}
		rise[Pluse] = temp_int;
		readPulse[Pluse] = TRUE;
		#else
		if((tmp_pulseB && !rise[Pluse]) || (!tmp_pulseB && rise[Pluse])) 
		{
			if(pluseValue[Pluse]>=0)
			{
				pluseValue[Pluse]++;
			}
			else
			{
				if(getRotaryEncoderDelay() == 0)
				{
					pluseValue[Pluse]=1;
				}
			}
		} 
		else 
		{
			if(pluseValue[Pluse]<=0)
			{
				pluseValue[Pluse]--;
			}
			else
			{
				if(getRotaryEncoderDelay() == 0)
				{
					pluseValue[Pluse]=-1;
				}
			}
		}
		readPulse[Pluse] = TRUE;
		#endif
		//printf("3.pluseValue[Pluse] = %d\r\n",pluseValue[Pluse]);
	
		
		
			
		
	}
	 
	#else
	char tmp_pulse_Dir;
	{
		KeyCount = 0;	
		for(i=0; i<80; i++)
		{
			for(nn=0; nn<100; nn++);	
			KeyNewInt = READ_PLUSE_INT(Pluse);
			KeyNewDir = READ_PLUSE_DIR(Pluse);
			if((KeyNewInt == KeyOldInt)&&(KeyNewDir == KeyOldDir))
			{
				if(++KeyCount > 50)
					break;
			}
			else 
			{
				KeyOldInt = KeyNewInt;
				KeyOldDir = KeyNewDir;
				KeyCount = 0;
			}
		}	
		
		if(READ_PLUSE_INT(Pluse) == 0)
		{
			//if(i == 80) return;
			tmp_pulse_Dir=READ_PLUSE_DIR(Pluse);

			if(tmp_pulse_Dir == 0)
			{
				if(pluseValue[Pluse]>=0)
				{
					pluseValue[Pluse]++;
				}
				else
				{
					pluseValue[Pluse]=1;
				}
			}
			else 
			{
				if(pluseValue[Pluse]<=0)
				{
					pluseValue[Pluse]--;
				}
				else
				{
					pluseValue[Pluse]=-1;
				}
			}

			// 20120808
			if(pluseValue[Pluse]>10)pluseValue[Pluse]+=100;
			else if(pluseValue[Pluse]>6)pluseValue[Pluse]+=10;
			else if(pluseValue[Pluse]<(-10))pluseValue[Pluse]-=100;
			else if(pluseValue[Pluse]<(-6))pluseValue[Pluse]-=10;
			
		}
		readPulse[Pluse] = TRUE;
	}
	#endif

	
	
}
void PLUSE_VNIC(u8 pluse)
{
	GPIO_InitTypeDef   GPIO_InitStructure;

	readPulseData(pluse);

	if(1)
	{
		if (readPulse[pluse]) 
		{
			if (rise[pluse]) 
			{
				GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
				rise[pluse] = FALSE;
			} 
			else 
			{
				GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
				rise[pluse] = TRUE;
			}
			// Configure EXTI Line
			GPIO_InitStructure.Pin = PULSE_INT_PIN[pluse];
	  		GPIO_InitStructure.Pull = GPIO_PULLUP;
	  		HAL_GPIO_Init(PULSE_INT_PORT[pluse], &GPIO_InitStructure);

			readPulse[pluse] = FALSE;
		}
		
	}
	
}

/*pulse*/
void init_Pluse(u8 Pluse)
{
  GPIO_InitTypeDef   GPIO_InitStructure;

  
  
  /* Configure PULSE pin as input floating */
  GPIO_InitStructure.Pin = PULSE_INT_PIN[Pluse];
  GPIO_InitStructure.Mode = PULSE_INT_MODE[Pluse];
  GPIO_InitStructure.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(PULSE_INT_PORT[Pluse], &GPIO_InitStructure);

  GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
  GPIO_InitStructure.Pin = PULSE_DIR_PIN[Pluse];
  HAL_GPIO_Init(PULSE_DIR_PORT[Pluse], &GPIO_InitStructure);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(PULSE_EXTI_IRQ[Pluse], 1, 0);
  HAL_NVIC_EnableIRQ(PULSE_EXTI_IRQ[Pluse]);
}

int GetpluseValue(u8 Pluse)
{
	int value;
	value = pluseValue[Pluse];
	//printf("GetpluseValue(),pluseValue[Pluse] = %d\r\n",value);
	pluseValue[Pluse] = 0;
	return value;	
}

/**
  * @brief  This function handles EXTI15_10_IRQ Handler.
  * @param  None
  * @retval None
  */


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	PLUSE_VNIC(pulse_A);
}



